package com.ruoyi.inventory.util;

import com.ruoyi.inventory.domain.TSpPart;
import com.ruoyi.inventory.domain.dto.PartDTO;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 备件转换工具类
 * 用于TSpPart和PartDTO之间的转换
 * 
 * @author ruoyi
 * @date 2025-01-15
 */
public class PartConverter 
{
    /**
     * TSpPart转换为PartDTO
     */
    public static PartDTO toDTO(TSpPart part) 
    {
        if (part == null) {
            return null;
        }
        
        PartDTO dto = new PartDTO();
        dto.setPartNo(part.getPartNo());
        dto.setOldPartNo(part.getOldPartNo());
        dto.setPartName(part.getName()); // 映射 name -> partName
        dto.setSpecNo(part.getSpecNo());
        dto.setPartTypeCode(part.getPartTypeCode());
        dto.setWarehouseCode(part.getWarehouseCode());
        dto.setImageUrls(part.getImageUrls());
        dto.setIsDelete(part.getIsDelete());
        dto.setCreatedAt(part.getCreatedAt());
        dto.setUpdatedAt(part.getUpdatedAt());
        dto.setRemark(part.getRemark());
        dto.setStockTotal(part.getStockTotal());
        dto.setAssociatedAssetsJson(part.getAssociatedAssetsJson());
        return dto;
    }

    /**
     * PartDTO转换为TSpPart
     */
    public static TSpPart toEntity(PartDTO dto) 
    {
        if (dto == null) {
            return null;
        }
        
        TSpPart part = new TSpPart();
        part.setPartNo(dto.getPartNo());
        part.setOldPartNo(dto.getOldPartNo());
        part.setName(dto.getPartName()); // 映射 partName -> name
        part.setSpecNo(dto.getSpecNo());
        part.setPartTypeCode(dto.getPartTypeCode());
        part.setWarehouseCode(dto.getWarehouseCode());
        part.setImageUrls(dto.getImageUrls());
        part.setIsDelete(dto.getIsDelete());
        part.setCreatedAt(dto.getCreatedAt());
        part.setUpdatedAt(dto.getUpdatedAt());
        part.setRemark(dto.getRemark());
        part.setAssociatedAssetsJson(dto.getAssociatedAssetsJson());
        return part;
    }

    /**
     * TSpPart列表转换为PartDTO列表
     */
    public static List<PartDTO> toDTOList(List<TSpPart> parts) 
    {
        if (parts == null) {
            return null;
        }
        
        return parts.stream()
                .map(PartConverter::toDTO)
                .collect(Collectors.toList());
    }

    /**
     * PartDTO列表转换为TSpPart列表
     */
    public static List<TSpPart> toEntityList(List<PartDTO> dtos) 
    {
        if (dtos == null) {
            return null;
        }
        
        return dtos.stream()
                .map(PartConverter::toEntity)
                .collect(Collectors.toList());
    }
}
